<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Models\GoldRound;
use Encore\Admin\Controllers\AdminController;

class GoldRoundsController extends AdminController
{
    protected $title = '金店场次';

    protected function grid()
    {
        $grid = new Grid(new GoldRound());
        $grid->model()->orderBy('id');

        $grid->column('id', 'ID');
        $grid->column('title', '标题');
        $grid->column('opened_at', '入场');
        $grid->column('started_at', '开场');
        $grid->column('ended_at', '闭场');
        $grid->column('is_close', '关闭状态')->display(function ($value) {
        	return $value ? _false() : _true();
        });
        $grid->column('is_ending', '清场状态')->display(function ($value) {
        	return $value ? '清场中...' : '活动中';
        });
        $grid->column('is_specail_round', '额外奖励场次')->display(function ($value) {
        	return $value ? _true() : _false();
        });

        $grid->disableRowSelector();
        $grid->disableColumnSelector();
        $grid->disableExport();
        $this->gridActions($grid);

        return $grid;
    }

	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableDelete();
		});
	}

    protected function form()
    {
        $form = new Form(new GoldRound());

        $form->text('title', '场次标记')->rules('required|string|unique:gold_rounds,title,{{id}}');
        $form->time('opened_at', '入场时间')->format('HH:mm:ss')->rules('required');
        $form->time('started_at', '开场时间')->format('HH:mm:ss')->rules('required');
        $form->time('ended_at', '闭场时间')->format('HH:mm:ss')->rules('required');
	    $form->radio('is_close', '关闭')->options(config('site.bool'))->default(false);
	    $form->radio('is_ending', '清场')->options(config('site.bool'))->default(false);
	    $form->radio('is_specail_round', '额外奖励')->options(config('site.bool'))->default(false);

        $this->formTools($form);
        $this->formSaving($form);

        return $form;
    }

	protected function formTools(Form $form)
	{
		$form->tools(function (Form\Tools $tools) {
			$tools->disableView();
			$tools->disableDelete();
		});
	}

	protected function formSaving(Form $form)
	{
		$form->saving(function (Form $form) {
			/*if ($form->isEditing() and ($form->model()->is_locking)) {
				admin_error('当前场次正在金店金店活动中...不可编辑!');
				return redirect('admin/gold-rounds');
			}*/
			return true;
		});
	}
}
